package com.lxkpt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxkpt.bean.User;
import com.lxkpt.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/adminManage")
@Controller
public class AdminManageController {

    @Resource
    private UserService userService;

    Map<Object, Object> map = new HashMap<>();

    @RequestMapping(value = "/addAdmin", method = RequestMethod.GET)
    @RequiresRoles("administrator")
    public String addAdmin() {
        return "admin-add";
    }

    /**
     * 新增管理员
     *
     * @return
     */
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    @RequiresRoles("administrator")
    @ResponseBody
    public Object addAdmin(User user) {
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), user.getUserName(), 3);
        user.setUserPassword(md5UserPassword.toString());
        user.setUserLevelId(2);
        int result = userService.insertSelective(user);
        if (result > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 搜索管理员页
     *
     * @return
     */
    @RequestMapping(value = "/searchAdmin", method = RequestMethod.GET)
    @RequiresRoles("administrator")
    public String searchUser() {
        return "admin-search";
    }

    /**
     * 搜索管理员
     *
     * @param pn
     * @param type
     * @param content
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchAdmin", method = RequestMethod.POST)
    @RequiresRoles("administrator")
    public String searchAdmin(@RequestParam(value = "pn", defaultValue = "1") String pn,
                              @RequestParam(value = "type") String type,
                              @RequestParam(value = "content") String content,
                              Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        /*content = content.trim().replaceAll(" ", "");
        List<User> userList = null;
        PageHelper.startPage(pageNo, 10);
        User user = new User();
        switch (type) {
            case "userName":
                user.setUserName(content);
                userList = userService.searchAdmin(user);
                break;
            case "userPhone":
                user.setUserPhone(content);
                userList = userService.searchAdmin(user);
                break;
            case "userEmail":
                user.setUserEmail(content);
                userList = userService.searchAdmin(user);
                break;
            default:
                break;
        }
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo", pageInfo);*/
        return "admin-search";
    }

    /**
     * 管理员列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/adminList")
    @RequiresRoles("administrator")
    public String listAdmin(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        User user = new User();
        user.setUserLevelId(2);
        List<User> adminList = userService.selectUser(user);
        PageInfo<User> pageInfo = new PageInfo<>(adminList);
        model.addAttribute("pageInfo", pageInfo);
        return "admin-manage";
    }
}
